package com.tianze.library.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.tianze.library.R;

import butterknife.ButterKnife;

/**
 * Created by kingtami.
 */
public class SimpleDialog extends Dialog implements OnClickListener {

    ProgressBar progressBar;
    TextView tvMessage;
    TextView tvCancel;
    TextView tvConfirm;
    LinearLayout layoutConfirm;

    private boolean mShowCancel;
    private String mCancelText;
    private String mConfirmText;
    private String mMessage;

    public static final int NORMAL_TYPE = 0;
    public static final int WARNING_TYPE = 1;
    public static final int PROGRESS_TYPE = 2;

    private int mAlertType;

    private OnSimpleClickListener mCancelClickListener;
    private OnSimpleClickListener mConfirmClickListener;

    public interface OnSimpleClickListener {
        void onClick(SimpleDialog simpleDialog);
    }

    public SimpleDialog(Context context) {
        this(context, NORMAL_TYPE);
    }

    public SimpleDialog(Context context, int alertType) {
        super(context, R.style.dialog);
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        mAlertType = alertType;
    }

    public SimpleDialog setMessageText(String text) {
        mMessage = text;
        if (tvMessage != null && mMessage != null) {
            tvMessage.setText(mMessage);
        }
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tiza_dialog_simple_alert);
        progressBar = ButterKnife.findById(this, R.id.progress_bar);
        tvMessage = ButterKnife.findById(this, R.id.tv_message);
        tvCancel = ButterKnife.findById(this, R.id.tv_cancel);
        tvConfirm = ButterKnife.findById(this, R.id.tv_confirm);
        layoutConfirm = (LinearLayout) findViewById(R.id.layout_confirm);
        tvCancel.setOnClickListener(this);
        tvConfirm.setOnClickListener(this);
        setMessageText(mMessage);
        setCancelText(mCancelText);
        setConfirmText(mConfirmText);
        changeAlertType(mAlertType);
    }

    public SimpleDialog setCancelClickListener(OnSimpleClickListener listener) {
        mCancelClickListener = listener;
        return this;
    }

    public SimpleDialog setConfirmClickListener(OnSimpleClickListener listener) {
        mConfirmClickListener = listener;
        return this;
    }

    public SimpleDialog setCancelText(String text) {
        mCancelText = text;
        if (tvCancel != null && mCancelText != null) {
            showCancelButton(true);
            tvCancel.setText(mCancelText);
        }
        return this;
    }

    public SimpleDialog showCancelButton(boolean isShow) {
        mShowCancel = isShow;
        if (tvCancel != null) {
            tvCancel.setVisibility(mShowCancel ? View.VISIBLE : View.GONE);
        }
        return this;
    }

    public SimpleDialog setConfirmText(String text) {
        mConfirmText = text;
        if (tvConfirm != null && mConfirmText != null) {
            tvConfirm.setText(mConfirmText);
        }
        return this;
    }

    private void changeAlertType(int alertType) {
        mAlertType = alertType;
        switch (mAlertType) {
            case WARNING_TYPE:
                tvCancel.setVisibility(View.GONE);
                break;
            case PROGRESS_TYPE:
                progressBar.setVisibility(View.VISIBLE);
                layoutConfirm.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_confirm) {
            if (mConfirmClickListener != null) {
                mConfirmClickListener.onClick(SimpleDialog.this);
            } else {
                SimpleDialog.super.dismiss();
            }
        } else if (v.getId() == R.id.tv_cancel) {
            if (mCancelClickListener != null) {
                mCancelClickListener.onClick(SimpleDialog.this);
            } else {
                SimpleDialog.super.dismiss();
            }
        }
    }
}
